package ru.usu.gv.styles;

import org.w3c.dom.Document;

import ru.usu.gv.utils.XmlUtil;

import java.util.HashMap;
import java.util.Map;

/**
 @author Sergey Pupyrev 
 25.03.2007
 */

public class Style
{
    private String fileName;
    private Map<String, String> values = new HashMap<String, String>();

    public Style(String fileName)
    {
        this.fileName = fileName;
    }

    private Document root = null;

    private Document getDocumentRoot()
    {
        try
        {
            if (root == null)
                root = XmlUtil.loadXmlFile(fileName);

            return root;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public String getStringAttribute(String path)
    {
        if (!values.containsKey(path))
            values.put(path, XmlUtil.readXmlValue(getDocumentRoot(), path));

        return values.get(path);
    }

    public double getDoubleAttribute(String path)
    {
        String value = getStringAttribute(path);
        return Double.parseDouble(value);
    }

    public int getIntegerAttribute(String path)
    {
        String value = getStringAttribute(path);
        return Integer.parseInt(value);
    }

    public boolean getBooleanAttribute(String path)
    {
        String value = getStringAttribute(path);
        return Boolean.parseBoolean(value);
    }

    public void setStringAttribute(String path, String value)
    {
        values.put(path, value);
    }

    public void setDoubleAttribute(String path, double value)
    {
        setStringAttribute(path, String.valueOf(value));
    }

    public void setIntegerAttribute(String path, int value)
    {
        setStringAttribute(path, String.valueOf(value));
    }

    public void setBooleanAttribute(String path, boolean value)
    {
        setStringAttribute(path, String.valueOf(value));
    }

}
